import { ref } from "vue";

import { onUnload, onShow } from "@dcloudio/uni-app";

const chooseLocation = requirePlugin("chooseLocation");

export function chooseLocationMixins(proxy) {
  const locationInfo = ref(null);

  function getMapFn() {
    uni.setStorageSync("is-choose-location", 1);
    let key = "GQPBZ-JKBWQ-I6B5C-B6WJV-ADPRH-PBF6D"; ///
    let referer = "智慧医疗";
    let location = {};
    uni.getLocation({
      type: "wgs84",
      geocode: true,
      success: (res) => {
        location = {
          latitude: res.latitude,
          longitude: res.longitude,
        };
        proxy.utils.uni.route.go(
          "plugin://chooseLocation/index?key=" +
            key +
            "&referer=" +
            referer +
            "&location=" +
            JSON.stringify(location)
        );
      },
      fail: () => {
        proxy.utils.uni.route.go(
          "plugin://chooseLocation/index?key=" +
            key +
            "&referer=" +
            referer +
            "&location=" +
            JSON.stringify(location)
        );
      },
    });
  }

  onShow(() => {
    locationInfo.value = chooseLocation.getLocation();
  });

  onUnload(() => {
    chooseLocation.setLocation(null);
  });

  return {
    getMapFn,
    locationInfo,
  };
}
